<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script type="text/javascript">
        // 手写reduce
        Array.prototype.chuanqingReduce = function (fn, count) {
            for (var i = 0; i < this.length; i++) {
                count = fn(count, this[i])
            }
            return count
        }

        const result = [1, 2].reduce((prev, element) => {
            return prev + element
        }, 0)
        console.log(result);
        const result1 = [1, 2, 3].chuanqingReduce((prev, element) => {
            return prev + element
        }, 0)
        console.log(result1);

        // Promise 

        // 手写async函数
        function chuanqingAsync(fn) {
            return Promise.resolve().then(() => {
                fn()
            })
        }

        // 测试resolve()的返回值  结果:undefined
        let p = new Promise((resolve, reject) => {
            resolve()
        })
        console.log(p);

        // 测试try catch 对async的影响
        let p2 = new Promise((resolve, reject) => {
            // setTimeout(() => {
            //     // reject('@@_error')

            // }, 5000)

            reject('nihao')
        })

        // try {
        //     p2.then(value => {
        //     })
        //     console.dir(p2)
        // } catch (error) {
        //     console.log(error);
        // }

        async function fn2() {
            try {
                p2.then(value => {
                    
                })
            } catch (error) {
                console.log(error);
            }
        }
        fn2()

    </script>
</body>

</html>